package edu.njfu.myapplication1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import android.content.Intent
import android.graphics.BitmapFactory
import android.util.Log
import androidx.appcompat.app.AlertDialog
import edu.njfu.homeork.model.ResultVO
import edu.njfu.homeork.service.StudentService
import edu.njfu.homeork.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import java.io.ByteArrayOutputStream
import java.io.InputStream

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var intentOK = Intent(this, StudentsInfo::class.java)

        loginBtn.setOnClickListener{
            val studentService=ServerCreator.create(StudentService::class.java)
            Log.d("dsfs",RegisterAccount.text.toString()+"   "+RegisterPassword.text.toString())
            studentService.getUserInfo(RegisterAccount.text.toString(),RegisterPassword.text.toString()).enqueue(object :retrofit2.Callback<ResultVO>{
                override fun onFailure(call: retrofit2.Call<ResultVO>, t: Throwable) {
                    TODO("Not yet implemented")
                }
                override fun onResponse(
                    call: retrofit2.Call<ResultVO>,
                    response: retrofit2.Response<ResultVO>
                ) {
                    var resultVO=response.body()
                    if(resultVO?.msg.toString()=="ok"){
                        startActivity(intentOK)
                    }else{
                        AlertDialog.Builder(this@MainActivity).apply {
                            setTitle("登录失败")
                            setMessage("账号密码错误")
                            setCancelable(false)
                            setPositiveButton("ok"){
                                    dialog, which ->
                            }
                            show()
                        }

                    }
                }
            })

        }
        regBtn.setOnClickListener{
            var intent1=Intent(this,RegisterActivity::class.java)
            startActivity(intent1)
        }
    }


}

